package com.superhelper.app.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.app.domain.Admin;
import com.superhelper.app.domain.AuserShopAudio;
import com.superhelper.app.domain.InvitationCode;
import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.domain.LoginLog;

/**
 * AdminMapper
 * 
 * @author Administrator
 *
 */

public interface AdminMapper {

    @Options(useGeneratedKeys = true, keyProperty = "a.id")
    @Insert("insert into auser(username,pwd,imid)values(#{a.username},#{a.pwd},#{a.imid})")
    int addUser(@Param("a") Admin a);

    // 绑定角色
    @Insert("insert into auser_role(auserId,roleId)values(#{userId},#{roleId})")
    int addUserRole(@Param("userId") Integer userId, @Param("roleId") Integer roleId);

    /**
     * 从数据库获取用户对象
     * 
     * @param username
     * @return
     */
    @Select("select * from auser where username = #{username}")
    Admin getAdminByUsername(@Param("username") String username);

    @Update("update auser set loginAt=#{time} where id=#{id}")
    int updateLoginAt(@Param("id") Long id, @Param("time") Date time);

    /**
     * 从数据库获取用户对象
     * 
     * @param username
     * @return
     */
    @Select("select * from auser where id = #{id}")
    Admin getAdminById(@Param("id") Long id);

    /**
     * 更新密码
     * 
     * @return
     */
    @Update("update auser set pwd=#{pwd} where username=#{phone}")
    int updatePwd(@Param("phone") String phone, @Param("pwd") String pwd);

    /**
     * 更新包名
     * 
     * @return
     */
    @Update("update auser set packageName=#{packageName},plfFrom=#{plfFrom} where id=#{auserId}")
    int updatePackageName(@Param("packageName") String packageName, @Param("plfFrom") String plfFrom,
            @Param("auserId") Long auserId);

    @Select("select * from auser_shop where id=#{id}")
    AuserShop getAuserShopById(@Param("id") Long id);

    @Select("select * from auser_shop where shopId=#{shopId} and shop_type=#{shop_type}")
    AuserShop getAuserShopByType(@Param("shopId") String shopId, @Param("shop_type") String shop_type);

    // 绑定商铺
    @Options(useGeneratedKeys = true, keyProperty = "a.id")
    @Insert("replace into auser_shop(auserId,shopId,shop_type,token,state,source)values(#{a.auserId},#{a.shopId},#{a.shop_type},#{a.token},#{a.state},#{a.source})")
    int addAuserShop(@Param("a") AuserShop a);

    @Update("update auser_shop set token=#{token} ,state=1,auserId=#{auserId} where shopId=#{shopId} and shop_type=#{shop_type}")
    int updateShopToken(@Param("auserId") Integer auserId, @Param("shopId") String shopId,
            @Param("shop_type") String shop_type, @Param("token") String token);

    @Update("update auser_shop set token=#{token} ,state=1 where shopId=#{shopId} and shop_type=#{shop_type}")
    int reflushShopToken(@Param("shopId") String shopId, @Param("shop_type") String shop_type,
            @Param("token") String token);

    @Select("select * from auser_shop where auserId=#{auserId}")
    List<AuserShop> getAuserShopsByAuserId(@Param("auserId") Long auserId);

    @Update("update auser_shop set lon=#{lon},lat=#{lat},partnerId=#{partnerId} where shopId=#{shopId} and shop_type=#{shop_type}")
    int updateShopPoi(@Param("shopId") String shopId, @Param("shop_type") String shop_type, @Param("lat") Double lat,
            @Param("lon") Double lon, @Param("partnerId") String partnerId);

    @Update("update auser_shop set orderAuto=#{a.orderAuto},deliverType=#{a.deliverType} where id=#{a.id}")
    int updateOrderConf(@Param("a") AuserShop shop);

    @Select("select * from auser_shop_audio where auserId=#{auserId} order by seq asc")
    List<AuserShopAudio> getAuserAudioByAuserId(@Param("auserId") Long auserId);

    @Update("update auser_shop set deliverType=#{deliverType} where id=#{id}")
    int updateDeliverSwitch(@Param("id") Long id, @Param("deliverType") int deliverType);

    @Insert("replace into auser_shop_audio (auserId,title,type,count,src,seq)value(#{a.auserId},#{a.title},#{a.type},#{a.count},#{a.src},#{a.seq})")
    int addAudio(@Param("a") AuserShopAudio entity);

    /**
     * 添加登录日志
     * 
     * @param ll
     * @return
     */
    @Insert("insert into auser_login_log(userId,ip,userAgent,device,version,model,deviceAgent)values"
            + "(#{ll.userId},#{ll.ip},#{ll.userAgent},#{ll.device},#{ll.version},#{ll.model},#{ll.deviceAgent})")
    int addLoginLog(@Param("ll") LoginLog ll);

    @Select("select DISTINCT(auserId) from auser_shop where shopId in(${ids})")
    List<Long> getAuserIdByShopIds(@Param("ids") String ids);

    @Select("select * from auser where id in(${ids})")
    List<Admin> getAdminByIds(@Param("ids") String ids);

    @Select("select * from invitation_code where auserId=#{auserId} order by updateAt desc limit 1")
    InvitationCode getInvitationCodeByUserId(@Param("auserId") Long auserId);

    @Select("select * from invitation_code where code=#{code}")
    InvitationCode getInvitationCodeByCode(@Param("code") String code);

    @Update("update invitation_code set state=1 ,auserId=#{auserId},updateAt = now() where code=#{code}")
    int updateCode(@Param("auserId") Long auserId, @Param("code") String code);

    @Update("update auser set expireAt=#{expireAt} where id=#{userId}")
    int updateUserExpireAt(@Param("userId") Long auserId, @Param("expireAt") Date expireAt);

    @Insert("insert into invitation_code (code,plfFrom,expireType) select #{a.code},#{a.plfFrom},#{a.expireType} from dual WHERE not exists (select * from invitation_code where code=#{a.code})")
    int addCode(@Param("a") InvitationCode code);

    @Select("select * from auser where packageName='com.superhelper.superaide' and expireAt>=date_sub(curdate(),interval 0 day)")
    List<Admin> getAdminTipsAt();

}
